﻿using Apps.Common;
using Apps.Models;
using System.Linq;
using System.Collections.Generic;
using Apps.Models.PUB;
using Apps.Models.RC;
using System.Linq.Expressions;
using System;
using LinqKit;

namespace Apps.BLL.RC
{
    public partial class RC_PROCESSBLL
    {
        public override List<RC_PROCESSModel> GetPagerList(ref GridPager pager, Expression<Func<RC_PROCESS, bool>> whereLambda)
        {

            IQueryable<RC_PROCESS> queryData = m_Rep.GetList(whereLambda);

            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

        public List<V_RC_PROCESSModel> GetProcessList(ref GridPager pager, Expression<Func<V_RC_PROCESS, bool>> whereLambda)
        {
            using (DBContainer db = new DBContainer())
            {
                IQueryable<V_RC_PROCESS> queryData = db.V_RC_PROCESS.AsQueryable();
                queryData = queryData.Where(whereLambda.Compile()).AsQueryable();
                pager.totalRows = queryData.Count();
                //排序
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                return TransHelper.TransExpList<V_RC_PROCESS, V_RC_PROCESSModel>(queryData.ToList());
            }
        }

        public V_RC_PROCESSModel GetProcess(Expression<Func<V_RC_PROCESS, bool>> whereLambda)
        {
            using (DBContainer db = new DBContainer())
            {
                IQueryable<V_RC_PROCESS> queryData = db.V_RC_PROCESS.AsQueryable();
                queryData = queryData.Where(whereLambda.Compile()).AsQueryable();
                return TransHelper.TransExpList<V_RC_PROCESS, V_RC_PROCESSModel>(queryData.ToList()).FirstOrDefault();
            }
        }


        public List<V_RC_PARKING> GetParkingList(ref GridPager pager, Expression<Func<V_RC_PARKING, bool>> whereLambda)
        {
            using (DBContainer db = new DBContainer())
            {
                IQueryable<V_RC_PARKING> queryData = db.V_RC_PARKING.AsQueryable<V_RC_PARKING>();
                queryData= queryData.Where(whereLambda.Compile()).AsQueryable();
                pager.totalRows = queryData.Count();
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                return queryData.ToList();
            }
        }


        public List<V_RC_IN> GetInList(ref GridPager pager, Expression<Func<V_RC_IN, bool>> whereLambda)
        {
            using (DBContainer db = new DBContainer())
            {
                IQueryable<V_RC_IN> queryData = db.V_RC_IN.AsQueryable<V_RC_IN>();
                queryData=queryData.Where(whereLambda.Compile()).AsQueryable();
                pager.totalRows = queryData.Count();
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                return queryData.ToList();
            }
        }


        public List<V_RC_WEIGHING> GetWeighingList(ref GridPager pager, Expression<Func<V_RC_WEIGHING, bool>> whereLambda)
        {
            using (DBContainer db = new DBContainer())
            {
                IQueryable<V_RC_WEIGHING> queryData = db.V_RC_WEIGHING.AsQueryable<V_RC_WEIGHING>();
                queryData= queryData.Where(whereLambda.Compile()).AsQueryable();
                pager.totalRows = queryData.Count();
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                return queryData.ToList();
            }
        }

        public List<V_RC_SAMPLING> GetSamplingList(ref GridPager pager, Expression<Func<V_RC_SAMPLING, bool>> whereLambda)
        {
            using (DBContainer db = new DBContainer())
            {
                IQueryable<V_RC_SAMPLING> queryData = db.V_RC_SAMPLING.AsQueryable<V_RC_SAMPLING>();
                queryData = queryData.Where(whereLambda.Compile()).AsQueryable();
                pager.totalRows = queryData.Count();
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                return queryData.ToList();
            }
        }

        public List<V_RC_UPLOADING> GetupLoadingList(ref GridPager pager, Expression<Func<V_RC_UPLOADING, bool>> whereLambda)
        {
            using (DBContainer db = new DBContainer())
            {
                IQueryable<V_RC_UPLOADING> queryData = db.V_RC_UPLOADING.AsQueryable<V_RC_UPLOADING>();
                queryData = queryData.Where(whereLambda.Compile()).AsQueryable();
                pager.totalRows = queryData.Count();
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                return queryData.ToList();
            }
        }

        public virtual List<RC_PROCESSModel> GetPagerListByComb(ref GridPager pager, Expression<Func<RC_PROCESS, bool>> whereLambda)
        {

            using (DBContainer db = new DBContainer())
            {

                List<string> materiel =(from m in db.PUB_MATERIEL
                                                    join t in db.PUB_MATERIEL_TYPE on m.MATERIEL_TYPE_ID equals t.MATERIEL_TYPE_ID
                                                    where t.IS_COAL==true
                                                    select m.MATERIEL_ID).ToList();

                IQueryable<RC_PROCESS> queryData = m_Rep.GetList().Where(p => materiel.Contains(p.MATERIEL_ID));
                queryData = queryData.AsExpandable().Where(whereLambda).AsQueryable();
                pager.totalRows = queryData.Count();
                //排序
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                return   CreateModelList(ref queryData);
            }
        }


        public List<V_RC_OUT> GetOutList(ref GridPager pager, Expression<Func<V_RC_OUT, bool>> whereLambda)
        {
            using (DBContainer db = new DBContainer())
            {
                IQueryable<V_RC_OUT> queryData = db.V_RC_OUT.AsQueryable<V_RC_OUT>();
                queryData = queryData.Where(whereLambda.Compile()).AsQueryable();
                pager.totalRows = queryData.Count();
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                return queryData.ToList();
            }
        }


        public override List<RC_PROCESSModel> CreateModelList(ref IQueryable<RC_PROCESS> queryData)
        {

            List<RC_PROCESSModel> modelList = (from r in queryData
                                                     select new RC_PROCESSModel
                                                     {
                                                         CAR_ID = r.CAR_ID,
                                                         DETAIL_ID = r.DETAIL_ID,
                                                         MATERIEL_ID = r.MATERIEL_ID,
                                                         MATERIEL_NAME = r.PUB_MATERIEL.MATERIEL_NAME,
                                                         MINE_ID = r.MINE_ID,
                                                         MINE_NAME = r.PUB_MINE.MINE_NAME,
                                                         OVERLOAD_RADOM = r.OVERLOAD_RADOM,
                                                         PROCESS_ID = r.PROCESS_ID,
                                                         STATUS = r.STATUS,
                                                         SUPPLIER_ID = r.SUPPLIER_ID,
                                                         SUPPLIER_NAME=r.PUB_SUPPLIER.SUPPLIER_NAME,
                                                         TRANSPORT_ID=r.TRANSPORT_ID,
                                                         TRANSPORT_NAME=r.PUB_TRANSPORT.TRANSPORT_NAME,
                                                         UPDATE_TIME=r.UPDATE_TIME,
                                                         CAR_NUMBER=r.PUB_CAR.LPR 
                                                     }).ToList();
            return modelList;
        }
    }
}